package com.ktgj.customer.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ktgj.common.core.constant.BusinessTypeConstants;
import com.ktgj.customer.domain.*;
import com.ktgj.customer.mapper.FinanceOrderRefundmentMapper;
import com.ktgj.customer.mapper.OrderInfoMapper;
import com.ktgj.customer.mapper.OrderPaymentRecord2Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.TravelOrderRefundMapper;
import com.ktgj.customer.service.ITravelOrderRefundService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-03
 */
@Service
public class TravelOrderRefundServiceImpl implements ITravelOrderRefundService 
{
    @Autowired
    private TravelOrderRefundMapper travelOrderRefundMapper;
    @Resource
    private FinanceOrderRefundmentMapper financeOrderRefundmentMapper;
    @Resource
    private OrderPaymentRecord2Mapper orderPaymentRecord2Mapper;
    @Resource
    private OrderInfoMapper orderInfoMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param orderrefundid 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public TravelOrderRefund selectTravelOrderRefundById(Long orderrefundid)
    {
        return travelOrderRefundMapper.selectTravelOrderRefundById(orderrefundid);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param travelOrderRefund 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TravelOrderRefund> selectTravelOrderRefundList(TravelOrderRefund travelOrderRefund)
    {
        return travelOrderRefundMapper.selectTravelOrderRefundList(travelOrderRefund);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param travelOrderRefund 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTravelOrderRefund(TravelOrderRefund travelOrderRefund)
    {
        return travelOrderRefundMapper.insertTravelOrderRefund(travelOrderRefund);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param travelOrderRefund 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateTravelOrderRefund(TravelOrderRefund travelOrderRefund)
    {
        int rows = 0;
        rows = travelOrderRefundMapper.updateTravelOrderRefund(travelOrderRefund);
        if (rows > 0)
        {
            TravelOrderRefund queryTravelOrderRefund = travelOrderRefundMapper.selectTravelOrderRefundById(travelOrderRefund.getOrderrefundid());
            FinanceOrderRefundment refundment = new FinanceOrderRefundment();
            refundment.setOrderTypeId(Long.valueOf(com.ktgj.common.core.constant.FinanceOrderRefundment.travel.getValue()));
            refundment.setCustomerId(queryTravelOrderRefund.getCustomerId());
            refundment.setRefundmentType(Long.valueOf(com.ktgj.common.core.constant.FinanceOrderRefundment.originalRoad.getValue()));
            refundment.setRefundmentAmount(queryTravelOrderRefund.getRefundamount());
            refundment.setStatus(com.ktgj.common.core.constant.FinanceOrderRefundment.initial.toString());
            refundment.setOrderId(String.valueOf(queryTravelOrderRefund.getOrderId()));
            refundment.setCreateTime(new Date());
            OrderPaymentRecord2Dto orderPaymentRecord2Dto = orderPaymentRecord2Mapper.selectOrderPaymentByOrderId(queryTravelOrderRefund.getOrderId());
            if (orderPaymentRecord2Dto != null)
            {
                if (orderPaymentRecord2Dto.getPayMethod() != null)
                {
                    if (orderPaymentRecord2Dto.getPayMethod().equals(BusinessTypeConstants.BankCredit.getValue()))
                    {
                        refundment.setPaymentTypeId("2");
                    } else if (orderPaymentRecord2Dto.getPayMethod().equals(BusinessTypeConstants.BankDebit.getValue()))
                    {
                        refundment.setPaymentTypeId("1");
                    }
                    if (orderPaymentRecord2Dto.getPayMethod().equals(BusinessTypeConstants.Wechat.getValue()))
                    {
                        refundment.setPaymentTypeId("3");
                    }
                }
                refundment.setSerialnumber(orderPaymentRecord2Dto.getTradeNo());
            }
            financeOrderRefundmentMapper.insertFinanceOrderRefundment(refundment);
        }
        return rows;
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param orderrefundids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteTravelOrderRefundByIds(Long[] orderrefundids)
    {
        return travelOrderRefundMapper.deleteTravelOrderRefundByIds(orderrefundids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param orderrefundid 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteTravelOrderRefundById(Long orderrefundid)
    {
        return travelOrderRefundMapper.deleteTravelOrderRefundById(orderrefundid);
    }
}
